#include "ImportDialog.h"
#include <QFileDialog>
#include <QMessageBox>
#include <QStyle>


ImportDialog::ImportDialog(QWidget *parent) : QDialog(parent)
{
	mainLayout = new QVBoxLayout(this);

	headLayout = new QHBoxLayout();
	buttonLayout = new QHBoxLayout();

	width = new LabelSpinBox("长", this);
	height = new LabelSpinBox("宽", this);
	frameCount = new LabelSpinBox("帧数", this);

	body = new FileInfoBox("body:  ", this);
	shadow = new FileInfoBox("shadow:", this);
	remap = new FileInfoBox("remap: ", this);

	qButton = new QPushButton(this);
	okButton = new QPushButton("导入", this);
	cancelButton = new QPushButton("取消", this);

	isFormZero = new QCheckBox("文件名从1开始", this);

	mainLayout -> addLayout(headLayout);
	mainLayout -> addWidget(body);
	mainLayout -> addWidget(shadow);
	mainLayout -> addWidget(remap);
	mainLayout -> addLayout(buttonLayout);

	headLayout -> addWidget(width);
	headLayout -> addWidget(height);
	headLayout -> addWidget(frameCount);
    headLayout -> addWidget(isFormZero);
	headLayout -> addWidget(qButton);
	headLayout -> setAlignment(Qt::AlignmentFlag::AlignCenter);

	buttonLayout -> addStretch();
	buttonLayout -> addWidget(okButton);
	buttonLayout -> addStretch();
	buttonLayout -> addWidget(cancelButton);
	buttonLayout -> setAlignment(Qt::AlignmentFlag::AlignCenter);
	buttonLayout -> addStretch();

	body -> checkBox -> setCheckState(Qt::CheckState::Checked);
	body -> checkBox -> setDisabled(true);
	body -> filenameBox -> lineEdit -> setText("01%04d.png");

	shadow -> filenameBox -> lineEdit -> setText("S01%04d.png");

	remap ->setDisabled(true);
	remap -> filenameBox -> lineEdit -> setText("01%04d.png");

	qButton -> setFixedSize(20, 20);
    qButton -> setIcon(style() -> standardIcon(QStyle::StandardPixmap::SP_DirOpenIcon));

	setLayout(mainLayout);

	setMinimumWidth(600);
	setMinimumHeight(300);

	connect(qButton, SIGNAL(clicked()), this, SLOT(OnQButtonClicked()));
	connect(okButton, SIGNAL(clicked()), this, SLOT(OnOkButtonClicked()));
	connect(cancelButton, SIGNAL(clicked()), this, SLOT(OnCancelButtonClicked()));
}

ImportDialog::~ImportDialog()
{
	delete width;
	delete height;
	delete frameCount;
	delete body;
	delete shadow;
	delete remap;
	delete qButton;
	delete okButton;
	delete cancelButton;
	delete headLayout;
	delete buttonLayout;
	delete mainLayout;
}

void ImportDialog::OnQButtonClicked()
{
	QString fileName = QFileDialog::getExistingDirectory(this, tr("打开目录"));
	body -> pathChoiceBox -> lineEdit -> setText(fileName + "/body");
	shadow -> pathChoiceBox -> lineEdit -> setText(fileName + "/shadow");
	remap -> pathChoiceBox -> lineEdit -> setText(fileName + "/remap");
}

void ImportDialog::OnOkButtonClicked()
{
	CurrentArgs.width = width -> spinBox -> value();
	CurrentArgs.height = height -> spinBox -> value();
	CurrentArgs.frameCount = frameCount -> spinBox -> value();

	CurrentArgs.isWithShadow = ((shadow -> checkBox -> checkState()) == Qt::CheckState::Checked);
	CurrentArgs.isWithRemap = ((remap -> checkBox -> checkState()) == Qt::CheckState::Checked);

	CurrentArgs.bodyPath = body -> pathChoiceBox -> lineEdit -> text();
	CurrentArgs.shadowPath = shadow -> pathChoiceBox -> lineEdit -> text();
	CurrentArgs.remapPath = remap -> pathChoiceBox -> lineEdit -> text();

	CurrentArgs.bodyFileName = ("/" + (body -> filenameBox -> lineEdit ->text()));
	CurrentArgs.shadowFileName = ("/" + (shadow -> filenameBox -> lineEdit ->text()));
	CurrentArgs.remapFileName = ("/" + (remap -> filenameBox -> lineEdit ->text()));

	CurrentArgs.isFileNameFormZero = isFormZero->checkState() != Qt::CheckState::Checked;

	try
	{
		CheckArgsOk();
		done(QDialog::Accepted);
	}
	catch (const StrException& e)
	{
		QMessageBox::warning(this, "参数错误", e.what());
	}
	catch (...)
	{
		QMessageBox::warning(this, "异常", "未知错误");
	}
}

void ImportDialog::OnCancelButtonClicked()
{
	done(QDialog::Rejected);
}

bool ImportDialog::CheckArgsOk() const
{
	if(CurrentArgs.width <= 0 || CurrentArgs.height <= 0)
		throw StrException("画布尺寸错误, 画布长和宽应当大于0.");
	if(CurrentArgs.frameCount <= 0)
		throw StrException("帧数错误, 帧数应当大于0.");
	char fileTmp[100];

	int fileSeqOffset = CurrentArgs.isFileNameFormZero ? 0 : 1;

	for(int i = 0; i < CurrentArgs.frameCount; i++)
	{
		sprintf(fileTmp, CurrentArgs.bodyFileName.toStdString().c_str(), i + fileSeqOffset);
		if(!Util::IsFileExist((CurrentArgs.bodyPath + fileTmp).toStdString()))
			throw StrException("文件: " + (CurrentArgs.bodyPath + fileTmp).toStdString() + "不存在.");
		if (CurrentArgs.isWithShadow)
		{
			sprintf(fileTmp, CurrentArgs.shadowFileName.toStdString().c_str(), i += fileSeqOffset);
			if(!Util::IsFileExist((CurrentArgs.shadowPath + fileTmp).toStdString()))
				throw StrException("文件: " + (CurrentArgs.shadowPath + fileTmp).toStdString() + "不存在.");
		}
		if (CurrentArgs.isWithRemap)
		{
			sprintf(fileTmp, CurrentArgs.remapFileName.toStdString().c_str(), i + fileSeqOffset);
			if(!Util::IsFileExist((CurrentArgs.remapPath + fileTmp).toStdString()))
				throw StrException("文件: " + (CurrentArgs.remapPath + fileTmp).toStdString() + "不存在.");
		}
	}
	return true;
}



